package com.demo.controller.admin;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.common.model.Article;
import com.demo.common.model.ArticleTag;
import com.demo.common.model.Tag;
import com.demo.controller.BaseController;
import com.demo.interceptor.DataInterceptor;
import com.demo.tools.ToolWeb;
import com.jfinal.aop.Before;
import com.jfinal.core.Action;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
/**
 * 
 * @author Emode
 * 标签管理
 */
public class TagAdminController extends BaseController<Tag> {
	public void index() {
		Integer page = getParaToInt("page",1);
		renderJson(Tag.me.paginate(page, 10,getQueryParam()));
	}

	// 保存
	public void saveOrUpdate() {
		Tag tag = new Tag();
		Boolean bool = false;
		String id = getPara("id","");
		tag.set("name", getPara("name"))
				.set("description", getPara("description"))
				.set("create_date", new Date());
		if(id!=null&&!("").equals(id)){
			bool = tag.set("id", id).update();
		}else{
			bool = tag.save();
		}
		if (bool) {
			success("添加成功");
		} else {
			error("添加失败");
		}
	}
	//删除操作
	public void del() {
		String id = getPara("id");
		Boolean bool = Tag.me.deleteById(id);
		if (bool) {
			success("删除成功");
		} else {
			error("删除失败");
		}
	}
	
	//跳转编辑页面
	public void toEdit() {
		String id = getPara("id");
		// Tag.me.findById(id);
		setAttr("tag", Tag.me.findById(id));
		render("/admin/article/tag-edit.html");
	}
	
/*	//更新操作
	public void update() {
		Tag tag = new Tag();
		Boolean bool = tag.set("id", getPara("id"))
				.set("name", getPara("name"))
				.set("description", getPara("description"))
				.set("create_date", new Date()).update();
		if (bool) {
			success("更新成功");
		} else {
			error("更新失败");
		}
	}*/

	//是否禁用
	public void operate() {
		String type = getPara("type");
		Boolean b;
		if ("yes".equals(type)) {
			b = true;
		} else {
			b = false;
		}
		Tag tag = new Tag();
		Boolean bool = tag.set("id", getPara("id")).set("status", b).update();
		if (bool) {
			success("操作成功");
		} else {
			error("操作失败");
		}
	}

	// 批量删除
	public void batchDel() {
		String[] ids = getPara("ids").split(",");
		Boolean bool = false;
		if (ids != null && ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				 List<ArticleTag> atlist = ArticleTag.me.getAllById("tag_id",ids[i]);
				 for(ArticleTag at:atlist){
					 bool =  at.delete();
				 }
				bool = Tag.me.deleteById(ids[i]);
			}

		}
		if (bool) {
			success("操作成功");
		} else {
			error("操作失败");
		}
	}

	// 搜索
	//@Before(DataInterceptor.class)
	public void search() {
		Integer page = getParaToInt("page");
		page = page==null?1:page;
		success(Tag.me.paginate(page, 10,getQueryParam()));
	}
	
	public void isUnique(){
		Map<String, Object> map = new HashMap<String, Object>();
		boolean bool = isUnique("t_tag", getPara("name"), getPara("param"));
		if(bool){
			map.put("info","该标签可以使用");
			map.put("status","y");
		}else{
			map.put("info","该标签不可使用");
			map.put("status","n");
			
		}
		renderJson(map);
	}


}
